
package com.gamecauca;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.gamecauca.common.Constant;
import com.gamecauca.common.OverlapTester;

public class Input implements InputProcessor {
	
	public static int X_MARKETBEGINLEFT = 140;
	public static int Y_MARKETBEGININTOP = 140;
	
	public static int X_BOXBEGININLEFT = 480;
	public static int Y_BOXBEGININTOP = 140;
	
	public static int CELL_HEIGHT = 55;
	public static int CELL_WIGHT = 55;
	public static int DISTANCETWOCELL = 10;
	public static int ROW_CELL = 3;
	public static int COL_CELL = 3;
	
	public static final int UP = 0;
	public static final int DOWN = 1;
	public static final int LEFT = 2;
	public static final int RIGHT = 3;
	
	public static final int CATCH = 4;
	public static final int SHOOT = 5;

	public static final int ESCAPE = 6;
	public static final int OPENDMARKET = 7;
	// khai bao cac bi tri touch trong man hinh marketcreen
	public static final int ZERO = 8;
	public static final int ONE = 9;
	public static final int TWO = 10;
	public static final int THREE = 11;
	public static final int FOUR = 12;
	public static final int FIVE = 13;
	public static final int SIX = 14;
	public static final int SEVEN = 14;
	public static final int EIGHT = 15;
	
	public static final int BOX_ZERO = 16;
	public static final int BOX_ONE = 17;
	public static final int BOX_TWO = 18;
	public static final int BOX_THREE = 19;
	public static final int BOX_FOUR = 20;
	public static final int BOX_FIVE = 21;
	public static final int BOX_SIX = 22;
	public static final int BOX_SEVEN = 23;
	public static final int BOX_EIGHT = 24;
	public static final int BUY = 25;
	public static final int SELL = 26;
	public static final int ITEM_ZERO = 27;
	public static final int ITEM_ONE = 28;
	public static final int ITEM_TWO = 29;
	public static final int ITEM_THREE = 30;
	public static final int ITEM_FOUR = 31;
	
	public static final int PLAYGAME = 32;
	public static final int ATTACK = 33;
	public static final int HIGHSCORE = 34;
	public static final int CREDITS = 35;
	
	public static final int CLASSICMODE = 36;
	public static final int NOMALMODE = 37;
	public static final int BACK = 38;
	
	public static final int OPEN_FACEBOOK = 39;
	public static final int OPEN_TWITTER = 40;
	public static final int EXITGAME = 41;
	
	// KET THUC PHAN KHAI BAO CAC VUNG TRONG MARKET
	public boolean[] buttons = new boolean[64];
	public boolean[] oldButtons = new boolean[64];
	public Rectangle [][] cellOfMarket = new Rectangle[ROW_CELL][COL_CELL];
	public Rectangle [][] cellOfBox = new Rectangle[ROW_CELL][COL_CELL];
	public Rectangle sellRectangle = new Rectangle(353, 287, 110,42);
	public Rectangle buyRectangle = new Rectangle(350, 143, 110,42);
	public Rectangle nextRectangle = new Rectangle(198, 93, 70,36);
	public Rectangle backRectangle = new Rectangle(198, 393, 70,36);
	
	public Input(){
	}

	public void set (int key, boolean down) {
		int button = -1;

		if (key == Keys.DPAD_UP) button = UP;
		if (key == Keys.DPAD_LEFT) button = LEFT;
		if (key == Keys.DPAD_DOWN) button = DOWN;
		if (key == Keys.DPAD_RIGHT) button = RIGHT;
		if (key == Keys.K) button = OPENDMARKET;
		if (key == Keys.H) button = ATTACK;

		if (key == Keys.Y) button = CATCH;
		if (key == Keys.Z) button = CATCH;
		if (key == Keys.X) button = SHOOT;
		if (key == Keys.C) button = CATCH;
		if (key == Keys.A) button = CATCH;
		if (key == Keys.S) button = SHOOT;
		if (key == Keys.D) button = CATCH;
		if (key == Keys.B) button = PLAYGAME;
		if (key == Keys.E) button = HIGHSCORE;
		if (key == Keys.U) button = CREDITS;
		if (key == Keys.F) button = OPEN_FACEBOOK;
		if (key == Keys.T) button = OPEN_TWITTER;
		if (key == Keys.F1) button = EXITGAME;
		
		if (key == Keys.ESCAPE || key == Keys.MENU) button = ESCAPE;

		if (button >= 0) {
			buttons[button] = down;
		}
	}

	public void tick (int screen) {
		for (int i = 0; i < buttons.length; i++) {
			oldButtons[i] = buttons[i];
		}

		if (Gdx.app.getType() == ApplicationType.Android) {
			if(screen == Constant.GameScreen){
				boolean left = false;
				boolean right = false;
				boolean z = false;
				boolean s = false;
				//System.out.println("gia tri cua A: ===================================== "+a  );
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y =480- (int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
//					System.out.println("Gdx.input.getX(i): ===================================== "+x  );
//					System.out.println("Gdx.input.getY(i): ===================================== "+y  );
//					System.out.println("(float)Gdx.graphics.getWidth(): ===================================== "+(float)Gdx.graphics.getWidth());
					if (!Gdx.input.isTouched(i)) continue;
					if (0<=x  && x < 150) {
//						System.out.println("Keys.DPAD_LEFT: ===================================== "  );
						set(Keys.DPAD_LEFT, true);
						left |= true;
					}
					else if (x >= 150 && x < 300) {
//						System.out.println("Keys.DPAD_RIGHT: ===================================== "  );
						set(Keys.DPAD_RIGHT, true);
						right |= true;
					}
					else if (x > 560 && x <= 640) {
//						System.out.println("set(Keys.Z, true): ===================================== "  );
						set(Keys.Z, true);
						z |= true;
					}
					else if ( x > 640 && x <= 700 && y > 0 && y < 100) {
//						System.out.println("set(Keys.X, true): ===================================== "  );
						set(Keys.H, true);
						s |= true;
					}
					else if (x > Constant.SCREEN_WIDTH-70 && x <= 800 &&y>= 306&&y<=370) {
						//System.out.println("y: ===================================== " +y );

						set(Keys.K, true);
						z |= true;
					}
					
					
					
				}

				if (left == false) set(Keys.DPAD_LEFT, false);
				if (right == false) set(Keys.DPAD_RIGHT, false);
				if (z == false) set(Keys.Z, false);
				if (s == false) set(Keys.X, false);
				return;
			}
			else if(screen == Constant.ShopItemsScreen){
				// truc toa do  y --tinh tu tren xuong
				//              x --tinh tu tren xuong
//				System.out.println("screen == Constant.ShopItemsScreen ===================================== "  );
				for(int i = 0;i<ROW_CELL;i++){
					for(int j =0;j<COL_CELL;j++){
						cellOfMarket[i][j] = new Rectangle(X_MARKETBEGINLEFT+CELL_WIGHT*i+DISTANCETWOCELL*i, Y_MARKETBEGININTOP+CELL_HEIGHT*j+DISTANCETWOCELL*j, CELL_WIGHT, CELL_HEIGHT);
						cellOfBox[i][j] = new Rectangle(X_BOXBEGININLEFT+CELL_WIGHT*i+DISTANCETWOCELL*i, Y_BOXBEGININTOP+CELL_HEIGHT*j+DISTANCETWOCELL*j, CELL_WIGHT, CELL_HEIGHT);
					}
				}
				
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y = (int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
//					System.out.println("Gdx.input.getX(i): ===================================== "+x  );
//					System.out.println("Gdx.input.getY(i): ===================================== "+y  );
					if (!Gdx.input.isTouched(i)) continue;
					for(int k = 0;k<ROW_CELL;k++){
						for(int j =0;j<COL_CELL;j++){
							if(OverlapTester.pointInRectangle(cellOfMarket[k][j], x,y)){
								System.out.println("cellOfMarket[i][j] ============ "+"k: "+k+"j: "+j);
							}
							else if(OverlapTester.pointInRectangle(cellOfBox[k][j], x,y)){
								System.out.println("cellOfBox[i][j]  ============"+"k: "+k+"j: "+j);
							}
						}
					}
					if(OverlapTester.pointInRectangle(buyRectangle, x,y)){
						System.out.println("buyRectangle ============ "+"x: "+x+"y: "+y);
					}
				    if(OverlapTester.pointInRectangle(sellRectangle, x,y)){
						System.out.println("sellRectangle ============ "+"x: "+x+"y: "+y);
					}
					if(OverlapTester.pointInRectangle(nextRectangle, x,y)){
						System.out.println("nextRectangle ============ "+"x: "+x+"y: "+y);
					}
					if(OverlapTester.pointInRectangle(backRectangle, x,y)){
						System.out.println("backRectangle ============ "+"x: "+x+"y: "+y);
					}
					
				}
				
			}
			
			else if(screen == Constant.MainMenuScreen){
				// truc toa do  y --tinh tu tren xuong
				//              x --tinh tu tren xuong
//				System.out.println("screen == Constant.ShopItemsScreen ===================================== "  );
//				
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y = (int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
//					System.out.println("X: " + x + " - Y: " + y);
					if (!Gdx.input.isTouched(i)) continue;
					if(Constant.checkCollision(x, y, 207, 146, 371, 238)){
						System.out.println("play game ne  ============ ============ ");
						set(Keys.B, true);
					}
					else if(Constant.checkCollision(x, y, 207, 248, 371, 328)){
						//System.out.println("play game ne  ============ ============ ");
						set(Keys.E, true);
					}
					else if(Constant.checkCollision(x, y, 207, 350, 371, 418)){
						set(Keys.U, true);
					}
					else if(Constant.checkCollision(x, y, 720, 360, 780, 400)){
						set(Keys.F, true);
					}
					else if(Constant.checkCollision(x, y, 620, 360, 680, 400)){
						set(Keys.T, true);
					}
					else if(Constant.checkCollision(x, y, 420, 360, 680, 420)){
						set(Keys.F1, true);
					}
					
				}
				
			}
			else if(screen == Constant.ChooseModeScreen){
				// truc toa do  y --tinh tu tren xuong
				//              x --tinh tu tren xuong
//				System.out.println("screen == Constant.ShopItemsScreen ===================================== "  );
//				
				for (int i = 0; i < 2; i++) {
					int x = (int)((Gdx.input.getX(i) / (float)Gdx.graphics.getWidth()) *800);
					int y = 480-(int)((Gdx.input.getY(i) / (float)Gdx.graphics.getHeight()) * 480);
					if (!Gdx.input.isTouched(i)) continue;
					if(Constant.checkCollision(x, y, 10, 200, 110, 300)){
//						System.out.println("classic ne ============ ============ ");
						set(Keys.F7, true);
					}
					else if(Constant.checkCollision(x, y, 500, 200, 600, 300)){
//						System.out.println("nomal nè  ============ ============ ");
						set(Keys.F8, true);
					}
					else if(Constant.checkCollision(x, y, 200, 40, 328, 120)){
						set(Keys.F9, true);
//						System.out.println("back ne ne  ============ ============ ");
					}
					
				}
			}
			
		}
	}

	public void releaseAllKeys () {
		for (int i = 0; i < buttons.length; i++) {
			if (i == UP || i == DOWN) continue;
			buttons[i] = false;
		}
	}

	@Override
	public boolean keyDown (int keycode) {
		set(keycode, true);
		return false;
	}

	@Override
	public boolean keyUp (int keycode) {
		set(keycode, false);
		return false;
	}

	@Override
	public boolean keyTyped (char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown (int x, int y, int pointer, int button) {
//		System.out.println("touchDown ====================================" );
//		if(screen == Constant.ChooseModeScreen){}
		return false;
	}

	@Override
	public boolean touchUp (int x, int y, int pointer, int button) {
//		System.out.println("touchUp ====================================" );
		x = (int)(x / (float)Gdx.graphics.getWidth() * 320);
		if (x > 160 - 32 && x < 160) {
//			System.out.println("x > 160 - 32 && x < 160 ====================================" );
			set(Keys.DPAD_UP, !buttons[UP]);
			if (buttons[UP]) buttons[DOWN] = false;
		}
		if (x > 160 && x < 160 + 32) {
//			System.out.println("x > 160 && x < 160 + 32 ====================================" );
			set(Keys.DPAD_DOWN, !buttons[DOWN]);
			if (buttons[DOWN]) buttons[UP] = false;
		}
//		System.out.println("buttons: " + buttons[UP] + ", " + buttons[DOWN]);
		return false;
	}

	@Override
	public boolean touchDragged (int x, int y, int pointer) {
		return false;
	}

	@Override
	public boolean touchMoved (int x, int y) {
		return false;
	}

	@Override
	public boolean scrolled (int amount) {
		return false;
	}
//	private boolean CheckTouch(GameControl region,int x,int y){
//		if(OverlapTester.pointInRectangle(region.bounds, x, y)) return true;
//		return false;
//	}
}
